/*******************************************************************************
 * @project: pdf_test
 * @package: com.burns.pdf
 * @file: T.java
 * @author: Administrator
 * @created: 2017-6-8
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/

package com.burns.pdf;

import java.io.File;
import java.util.Iterator;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.junit.Test;

public class PdfTest {

	public static void main(String[] args) {
		PdfTest pt = new PdfTest();
		String pdfFilePath = "C:/CHS-AVIDM4/Design1/Design1.pdf";
		String depositPath = "C:/CHS-AVIDM4/Design1/Design1_generate.pdf";
		int totalPage = 1;
		String pgStartPos = "L";
		int startPage = 1;
		int pageNum = 1;

		pt.generateSubPdf(pdfFilePath, depositPath, totalPage, pgStartPos,
				startPage, pageNum);
	}

	/**
	 *    * 根据起始页和文章页数截取PDF-何丽杰-2017-04-12    * pdfFilePath:PDF文件的全路径    *
	 * depositPath:新生成PDF的全路径    * totalPage:PDF文件的总页数    *
	 * pgStartPos:PDF文件的起始位置,L左边,R右边    * startPage:文章的起始页    * pageNum:文章的页数  
	 *  
	 */
	public boolean generateSubPdf(String pdfFilePath, String depositPath,
			int totalPage, String pgStartPos, int startPage, int pageNum) {
		boolean flag;
		flag = false;

		int stopPage = startPage + pageNum - 1;
		// 处理结束页
		String pgStopPos = null;
		if (stopPage % 2 == 0) {
			pgStopPos = "L";
		} else if (stopPage % 2 == 1) {
			pgStopPos = "R";
		}

		// 处理起始页
		if (startPage == 1) {
			startPage = 1;
			stopPage = 1;
		} else if (startPage == totalPage) {
			startPage = (startPage + 2) / 2;
			stopPage = startPage;
		} else {
			if (startPage % 2 == 1) {
				startPage = (startPage + 1) / 2;
			} else if (startPage % 2 == 0) {
				startPage = (startPage + 2) / 2;
			}
			if (stopPage % 2 == 1) {
				stopPage = (stopPage + 1) / 2;
			} else if (stopPage % 2 == 0) {
				stopPage = (stopPage + 2) / 2;
			}
		}
		// 处理总页数
		int pageCount = (totalPage + 2) / 2;
		// 截取PDF
		try {
			PDDocument helloDocument = PDDocument.load(new File(pdfFilePath));
			PDPageTree pdpt = helloDocument.getDocumentCatalog().getPages();
			Iterator<PDPage> it = pdpt.iterator();
			PDDocument doc = null;
			doc = new PDDocument();
			int i = 1;
			PDPage pdp;
			for (; it.hasNext();) {
				if (i <= stopPage && i >= startPage) {
					pdp = (org.apache.pdfbox.pdmodel.PDPage) it.next();
					PDRectangle r = pdp.getCropBox();
					if (startPage == 1 || startPage == pageCount) {
					} else {
						if (pageNum == 1) {
							if (pgStartPos.equals("R")) {
								r.setLowerLeftX(r.getUpperRightX() / 2);
								r.setLowerLeftY(0);
							}
							if (pgStartPos.equals("L")) {
								r.setUpperRightX(r.getUpperRightX() / 2);
								r.setUpperRightY(r.getUpperRightY());
							}
						} else {
							if (i == startPage) {
								if (pgStartPos.equals("R")) {
									r.setLowerLeftX(r.getUpperRightX() / 2);
									r.setLowerLeftY(0);
								}
							} else if (i == stopPage) {
								if (pgStopPos.equals("L")) {
									r.setUpperRightX(r.getUpperRightX() / 2);
									r.setUpperRightY(r.getUpperRightY());
								}
							}
						}
					}
					pdp.setCropBox(r);
					doc.addPage(pdp);
				} else {
					it.next();
				}
				i++;
			}
			doc.save(depositPath);
			doc.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Test
	public void dotest() {
		// String pdfFilePath,String depositPath,int totalPage,String
		// pgStartPos,int startPage, int pageNum
		String pdfFilePath = "C:\\Users\\丽杰\\Desktop\\测试文件.pdf";
		String depositPath = "D:\\test.pdf";
		generateSubPdf(pdfFilePath, depositPath, 12, "R", 3, 3);
	}
}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/
